package com.easylinkin.linkappapi.openapi.service.impl;

import com.easylinkin.linkappapi.openapi.service.ApplicationCopyDirectService;
import com.easylinkin.linkappapi.rulecomponent.entity.RuleComponent;
import com.easylinkin.linkappapi.rulecomponent.entity.RuleComponentAttribute;
import com.easylinkin.linkappapi.rulecomponent.entity.RuleComponentService;
import com.easylinkin.linkappapi.rulecomponent.mapper.RuleComponentAttributeMapper;
import com.easylinkin.linkappapi.rulecomponent.mapper.RuleComponentMapper;
import com.easylinkin.linkappapi.rulecomponent.mapper.RuleComponentServiceMapper;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @program: linkapp-group-cloud
 * @description: 规则组件库拷贝
 * @author: chenkaixuan
 * @create: 2021-09-07 11:15
 */
@Slf4j
@Order(1)
@Service
public class ApplicationCopyRuleComponentDirectServiceImpl implements ApplicationCopyDirectService {
    @Resource
    RuleComponentMapper ruleComponentMapper;
    @Resource
    RuleComponentAttributeMapper ruleComponentAttributeMapper;
    @Resource
    RuleComponentServiceMapper ruleComponentServiceMapper;

    @Override
    public RestMessage copy(HashMap<String, String> screenmap, HashMap<String, String> map, LinkappUser newLinkappUser, LinkappUser oldLinkappUser) {
        return null;
    }

    @Override
    public RestMessage copy(LinkappUser newLinkappUser, LinkappUser oldLinkappUser) {
        if (newLinkappUser == null || oldLinkappUser == null) {
            return RestBuilders.failureMessage().setMessage("用户为空");
        }
        RestMessage restMessage = RestBuilders.successBuilder().build();
        RuleComponent ruleComponent = new RuleComponent();
        ruleComponent.setTenantId(oldLinkappUser.getTenantId());
        List<RuleComponent> ruleComponents = ruleComponentMapper.getRuleComponents(ruleComponent);
        if (ruleComponents == null || ruleComponents.size() <= 0) {
            return restMessage;
        }
        int size = (int) (ruleComponents.size() * 0.75) + 1;
        HashMap<String, String> fatherSubMapper = new HashMap<>(size);
        Date date = new Date();
        ruleComponents = ruleComponents.stream().map(m -> {
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            fatherSubMapper.put(m.getId(), id);
            m.setId(id);
            m.setCreateTime(date);
            m.setModifyTime(date);
            m.setCreator(newLinkappUser.getId() + "");
            m.setModifier(newLinkappUser.getId() + "");
            m.setTenantId(newLinkappUser.getTenantId());
            return m;
        }).collect(Collectors.toList());

        //规则-服务
        RuleComponentService ruleComponentService = new RuleComponentService();
        ruleComponentService.setTenantId(oldLinkappUser.getTenantId());
        List<RuleComponentService> ruleComponentServices = ruleComponentServiceMapper.getRuleComponentServices(ruleComponentService);
        if (ruleComponentServices != null && ruleComponentServices.size() > 0) {
            ruleComponentServices = ruleComponentServices.stream().map(m -> {
                String id = UUID.randomUUID().toString().replaceAll("-", "");
                //set新父类
                if (fatherSubMapper.get(m.getId()) != null) {
                    m.setRuleComponentId(fatherSubMapper.get(m.getId()));
                }
                m.setId(id);
                m.setCreateTime(date);
                m.setModifyTime(date);
                m.setCreator(newLinkappUser.getId() + "");
                m.setModifier(newLinkappUser.getId() + "");
                m.setTenantId(newLinkappUser.getTenantId());
                return m;
            }).collect(Collectors.toList());
        }

        //规则-属性
        RuleComponentAttribute ruleComponentAttribute = new RuleComponentAttribute();
        ruleComponentAttribute.setTenantId(oldLinkappUser.getTenantId());
        List<RuleComponentAttribute> ruleComponentAttributes = ruleComponentAttributeMapper.getRuleComponentAttributes(ruleComponentAttribute);
        if (ruleComponentAttributes != null && ruleComponentAttributes.size() > 0) {
            size = (int) (ruleComponentAttributes.size() * 0.75) + 1;
            ruleComponentAttributes = ruleComponentAttributes.stream().map(m -> {
                String id = UUID.randomUUID().toString().replaceAll("-", "");
                //set新父类
                if (fatherSubMapper.get(m.getId()) != null) {
                    m.setRuleComponentId(fatherSubMapper.get(m.getId()));
                }
                m.setId(id);
                m.setCreateTime(date);
                m.setModifyTime(date);
                m.setCreator(newLinkappUser.getId() + "");
                m.setModifier(newLinkappUser.getId() + "");
                m.setTenantId(newLinkappUser.getTenantId());
                return m;
            }).collect(Collectors.toList());
        }
        //批量新增
        int batchSize = 100;
        if (ruleComponents != null && ruleComponents.size() > 0) {
            for (int idx = 0; idx < ruleComponents.size(); idx += batchSize) {
                List<RuleComponent> list = ruleComponents.subList(idx, Math.min(ruleComponents.size(), idx + batchSize));
                ruleComponentMapper.batchInsert(list);
            }
        }

        if (ruleComponentServices != null && ruleComponentServices.size() > 0) {
            for (int idx = 0; idx < ruleComponentServices.size(); idx += batchSize) {
                List<RuleComponentService> list = ruleComponentServices.subList(idx, Math.min(ruleComponentServices.size(), idx + batchSize));
                ruleComponentServiceMapper.batchInsert(list);
            }
        }
        if (ruleComponentAttributes != null && ruleComponentAttributes.size() > 0) {
            for (int idx = 0; idx < ruleComponentAttributes.size(); idx += batchSize) {
                List<RuleComponentAttribute> list = ruleComponentAttributes.subList(idx, Math.min(ruleComponentAttributes.size(), idx + batchSize));
                ruleComponentAttributeMapper.batchInsert(list);
            }
        }
        log.info("规则组件库拷贝完成");
        return restMessage;
    }
}
